package com.talent.jiwen.my;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

public class CardBindActivity extends BaseActivity {
    @BindView(R.id.cardEt)
    EditText cardEt;
    @BindView(R.id.cardDelIv)
    ImageView cardDelIv;
    @BindView(R.id.bindBtn)
    Button bindBtn;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_bind_idcard;
    }

    @Override
    public String titleName() {
        return "绑定身份证";
    }

    @Override
    protected void initData() {

        cardEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String card = cardEt.getText().toString();
                if (Validators.isEmpty(card)){
                    cardDelIv.setVisibility(View.GONE);
                }else{
                    cardDelIv.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @OnClick({R.id.cardDelIv, R.id.bindBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cardDelIv:
                if (!Validators.isEmpty(cardEt.getText().toString())){
                    cardEt.setText("");
                }
                break;
            case R.id.bindBtn:
                String cardNumber = cardEt.getText().toString();
                if (Validators.isEmpty(cardNumber)){
                    showToast("身份证号不能为空");
                }else{
                    bindStudentCard(cardNumber);
                }
                break;
        }
    }

    /**绑定身份证号
     *
     * @param cardNumber
     */
    private void bindStudentCard(String cardNumber){
        Map<String, String> map = new HashMap<>();
        map.put("IDNumber",  cardNumber);
        Observable observable = ApiFactory.getEducationApiSingleton().updateStudentIDNumber(SPUtil.getToken(), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(this) {
            @Override
            protected void _onNext(Void v) {
                showToast("绑定成功");
//                SPUtil.intIn(SPConstant.ID_CARD_BIND_STATUS , 1);
                EventBus.getDefault().post(CouponListFragment.REFRESH_COUPON_LIST);
                EventBus.getDefault().post(CouponSelectActivity.REFRESH_COUPON_SELECT);
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, true);
    }
}
